﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConvergensDominoConnector;

namespace CDCOSDH
{
    public partial class ActiveCasesByOffice : Form
    {
        private string sdhName;
        private string viewName;

        public List<string> selectedRow = new List<string>(10);

        public ActiveCasesByOffice(string sdhName, string viewName)
        {
            InitializeComponent();

            this.sdhName = sdhName;
            this.viewName = viewName;
            this.Text = "Alle aktive sager efter kontor i " + this.sdhName + " SDH";

            this.lvActiveCasesByOffice.Select();
        }

        private void ActiveCasesByOffice_Load(object sender, EventArgs e)
        {
            DataTable dataTable = DominoConnector.Default.GetActiveCasesByOffice(this.viewName, this.sdhName);
            //this.dataGridView1.DataSource = dataTable;

            this.lvActiveCasesByOffice.SuspendUpdate();

            this.lvActiveCasesByOffice.Groups.Clear();

            for (int a = 0; a < dataTable.Columns.Count; a++)
            {
                if (dataTable.Columns[a].ColumnName != "UNID")
                {
                    LidorSystems.IntegralUI.Lists.ListViewColumn columnsName = new LidorSystems.IntegralUI.Lists.ListViewColumn(dataTable.Columns[a].ColumnName);
                    this.lvActiveCasesByOffice.Columns.Add(columnsName);
                }
            }

            LidorSystems.IntegralUI.Lists.ListViewItem item = null;
            LidorSystems.IntegralUI.Lists.ListViewSubItem subItem = null;
            LidorSystems.IntegralUI.Lists.ListViewGroup group = null;

            foreach (DataRow dr in dataTable.Rows)
            {
                item = new LidorSystems.IntegralUI.Lists.ListViewItem();

                foreach (DataColumn dc in dataTable.Columns)
                {
                    subItem = new LidorSystems.IntegralUI.Lists.ListViewSubItem(dr[dc].ToString());
                    item.SubItems.Add(subItem);
                }

                this.lvActiveCasesByOffice.Items.Add(item);
            }

            var distinctGroups = (from Rows in dataTable.AsEnumerable()
                                  select Rows[0]).Distinct().ToList();

            for (int k = 0; k < distinctGroups.Count; k++)
            {
                if (distinctGroups[k].ToString().Equals(string.Empty))
                {
                    distinctGroups[k] = "Ikke kategorisereret";
                }

                group = new LidorSystems.IntegralUI.Lists.ListViewGroup(distinctGroups[k].ToString());
                this.lvActiveCasesByOffice.Groups.Add(group);
            }

            // Assign items to groups depending on their index
            int groupIndex = 0;
            List<string> addedGroup = new List<string>(500);

            addedGroup.Add(this.lvActiveCasesByOffice.Items[0].SubItems[0].Text);

            for (int i = 0; i < this.lvActiveCasesByOffice.Items.Count; i++)
            {
                if (addedGroup.Exists(element => element.Equals(this.lvActiveCasesByOffice.Items[i].SubItems[0].Text)))
                {
                    foreach (DataColumn dc in dataTable.Columns)
                    {
                        if (dc.ColumnName == "Column1")
                        {
                            this.lvActiveCasesByOffice.Items[i].SubItems[0].Text = string.Empty;
                            continue;
                        }
                        else
                        {
                            this.lvActiveCasesByOffice.Groups[groupIndex].Items.Add(this.lvActiveCasesByOffice.Items[i]);
                        }

                    }
                    continue;
                }
                else
                {
                    groupIndex++;

                    addedGroup.Add(this.lvActiveCasesByOffice.Items[i].SubItems[0].Text);

                    foreach (DataColumn dc in dataTable.Columns)
                    {
                        if (dc.ColumnName == "Column1")
                        {
                            this.lvActiveCasesByOffice.Items[i].SubItems[0].Text = string.Empty;
                            continue;
                        }
                        else
                        {
                            this.lvActiveCasesByOffice.Groups[groupIndex].Items.Add(this.lvActiveCasesByOffice.Items[i]);
                        }
                    }
                }
            }

            addedGroup.Clear();
            addedGroup = null;


            int listWidth = this.lvActiveCasesByOffice.ContentPanel.Width;
            int colSpace = listWidth / this.lvActiveCasesByOffice.Columns.Count;
            foreach (LidorSystems.IntegralUI.Lists.ListViewColumn column in this.lvActiveCasesByOffice.Columns)
            {
                column.Width = colSpace;
            }

            this.lvActiveCasesByOffice.ShowGroups = true;
            this.lvActiveCasesByOffice.ResumeUpdate();
            this.lvActiveCasesByOffice.Columns[0].HeaderText = "Kontor";
            this.lvActiveCasesByOffice.Columns[0].Width = 43;
            this.lvActiveCasesByOffice.Columns[1].Width = 80;
            this.lvActiveCasesByOffice.Columns[2].Width = 420;
            this.lvActiveCasesByOffice.Columns[3].Width = 200;

            for (int i = 0; i < this.lvActiveCasesByOffice.Groups.Count; i++)
            {
                LidorSystems.IntegralUI.Lists.ListViewGroup listViewGroup = this.lvActiveCasesByOffice.Groups[i];
                listViewGroup.Collapse();
            }

            this.lvActiveCasesByOffice.Invalidate();

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            LidorSystems.IntegralUI.Lists.ListViewItem selectedItem = this.lvActiveCasesByOffice.SelectedItem;

            if (selectedItem != null)
            {
                this.selectedRow.Add(selectedItem.SubItems[1].Text);
                this.selectedRow.Add(selectedItem.SubItems[2].Text);
                this.selectedRow.Add(selectedItem.SubItems[4].Text);
                
                this.DialogResult = DialogResult.OK;
            }
        }

        private void lvActiveCasesByOffice_DoubleClick(object sender, EventArgs e)
        {
            LidorSystems.IntegralUI.Lists.ListViewItem selectedItem = this.lvActiveCasesByOffice.SelectedItem;

            if (selectedItem != null)
            {
                this.selectedRow.Add(selectedItem.SubItems[1].Text);
                this.selectedRow.Add(selectedItem.SubItems[2].Text);
                this.selectedRow.Add(selectedItem.SubItems[4].Text);
                
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCnl_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void lvActiveCasesByOffice_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.lvActiveCasesByOffice.SelectedItems.Count == 0)
            {
                return;
            }
            if (e.KeyCode == Keys.Enter)
            {
                LidorSystems.IntegralUI.Lists.ListViewItem selectedItem = this.lvActiveCasesByOffice.SelectedItem;

                this.selectedRow.Add(selectedItem.SubItems[1].Text);
                this.selectedRow.Add(selectedItem.SubItems[2].Text);
                this.selectedRow.Add(selectedItem.SubItems[4].Text);

                this.DialogResult = DialogResult.OK;
            }
        }
    }
}

